if (BX.namespace('ProfiStudio.Site.Clipboard')) {
    const CopyButton = function (copyableElement) {
        this._element = copyableElement;
        this._bindEvents();
    };

    CopyButton.prototype.copyText = function (text) {
        navigator.clipboard.writeText(text).then(() => {
            BX.onCustomEvent('onTextCopy', [this._element, text]);

            this._element.classList.add('active');
            setTimeout(() => {
                this._element.classList.remove('active');
            }, 2000);
        });
    };

    CopyButton.prototype._bindEvents = function () {
        const btn = this._element.querySelector('.copy');
        if (btn) {
            btn.addEventListener('click', (event) => {
                this.copyText(this._element.innerText);
                event.preventDefault();
            });
        }
        this._element.addEventListener('click', (event) => {
            event.preventDefault();
            return false;
        });
    };

    BX.ProfiStudio.Site.Clipboard.CopyButton = CopyButton;
}

document.addEventListener('DOMContentLoaded', () => {
    document.querySelectorAll('.copyable')
        .forEach((button) => {
            new BX.ProfiStudio.Site.Clipboard.CopyButton(button);
        });
});